package com.group7.mall.service.impl;

import com.group7.mall.convert.GoodsConverter;
import com.group7.mall.convert.StoreConverter;
import com.group7.mall.dao.*;
import com.group7.mall.dto.*;
import com.group7.mall.entity.*;
import com.group7.mall.req.SellerGoodsPageRequest;
import com.group7.mall.resp.SellerGoodsPageResponse;
import com.group7.mall.resp.UserGoodsPageResponse;
import com.group7.mall.req.UserGoodsPageRequest;
import com.group7.mall.service.UserGoodsService;
import com.group7.mall.util.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.*;

@Service
 public class UserUserGoodsServiceImpl implements UserGoodsService {

    @Resource
    private TbGoodsDao tbGoodsDao;

    @Resource
    private TbGoodsImageDao tbGoodsImageDao;

    @Resource
    private TbGoodsParameterDao tbGoodsParameterDao;

    @Resource
    private TbUserCommentDao tbUserCommentDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbGoodsAttributeDao tbGoodsAttributeDao;

    @Resource
    private TbGoodsTypeDao tbGoodsTypeDao;

    @Resource
    private TbGoodsInventoryPriceDao tbGoodsInventoryPriceDao;

    @Resource
    private TbBrowsingHistoryDao tbBrowsingHistoryDao;

    @Resource
    private RedisUtil redisUtil;

    @Value("${redis.keys.goods-history-key}")
    private String GOODS_HISTORY_KEY;

    @Override
    public UserGoodsPageResponse<GoodsPreviewDto> queryUserBrowsingHistory(UserGoodsPageRequest userGoodsPageRequest) {
        userGoodsPageRequest.setPageStart((userGoodsPageRequest.getPageNo() - 1) * userGoodsPageRequest.getPageSize());
        List<TbBrowsingHistory> tbBrowsingHistoryList = tbBrowsingHistoryDao.queryAllByLimit(userGoodsPageRequest);
        List<GoodsPreviewDto> goodsPreviewDtoList = new ArrayList<>();
        for (TbBrowsingHistory x : tbBrowsingHistoryList) {
            TbGoods tbGoods = tbGoodsDao.queryById(x.getGoodsId());
            goodsPreviewDtoList.add(GoodsConverter.INSTANCE.convertTbGoodsToGoodsPreviewDto(tbGoods));
        }
        UserGoodsPageResponse<GoodsPreviewDto> userGoodsPageResponse = new UserGoodsPageResponse<>();
        userGoodsPageResponse.setPageSize(userGoodsPageRequest.getPageSize());
        userGoodsPageResponse.setCurrent(userGoodsPageRequest.getPageNo());
        userGoodsPageResponse.setRecords(goodsPreviewDtoList);
        TbBrowsingHistory tbBrowsingHistory = new TbBrowsingHistory();
        tbBrowsingHistory.setUserOpenId(userGoodsPageRequest.getUserOpenId());
        userGoodsPageResponse.setTotal(tbBrowsingHistoryDao.count(tbBrowsingHistory));
        return userGoodsPageResponse;
    }

    @Override
    public StoreDto showStore(TbSeller tbSeller) {
        return StoreConverter.INSTANCE.convertTbSellerToStoreDto(tbSellerDao.queryById(tbSeller.getStoreId()));
    }

    @Override
    public List<GoodsPreviewDto> showRecommendedGoods() {
        List<TbGoods> tbGoodsList = tbGoodsDao.queryByIsRecommendation();
        return GoodsConverter.INSTANCE.convertTbGoodsListToGoodsPreviewDtoList(tbGoodsList);
    }


    @Override
    public UserGoodsPageResponse<GoodsPreviewDto> queryGoodsPreviewByPage(UserGoodsPageRequest userGoodsPageRequest) {
        userGoodsPageRequest.setPageStart((userGoodsPageRequest.getPageNo() - 1) * userGoodsPageRequest.getPageSize());
        List<TbGoods> tbGoodsList = tbGoodsDao.queryAllByLimit(userGoodsPageRequest);
        for (TbGoods x : tbGoodsList) {
            System.out.println(x.toString());
        }
        List<GoodsPreviewDto> goodsPreviewDtoList = GoodsConverter.INSTANCE.convertTbGoodsListToGoodsPreviewDtoList(tbGoodsList);
        UserGoodsPageResponse<GoodsPreviewDto> userGoodsPageResponse = new UserGoodsPageResponse<>();
        userGoodsPageResponse.setPageSize(userGoodsPageRequest.getPageSize());
        userGoodsPageResponse.setCurrent(userGoodsPageRequest.getPageNo());
        userGoodsPageResponse.setRecords(goodsPreviewDtoList);
        userGoodsPageResponse.setTotal(tbGoodsDao.countByLimit(userGoodsPageRequest));
        return userGoodsPageResponse;
    }

    @Override
    public GoodsPreviewDto queryGoodsPreview(Integer goodsId) {
        TbGoods tbGoods = tbGoodsDao.queryById(goodsId);
        return GoodsConverter.INSTANCE.convertTbGoodsToGoodsPreviewDto(tbGoods);
    }

    @Override
    public TbGoods insert(TbGoods tbGoods) {
        this.tbGoodsDao.insert(tbGoods);
        return tbGoods;
    }

    @Override
    public UserGoodsDetailsDto showGoodsDetail(Integer goodsId) {
        TbGoods tbGoods = tbGoodsDao.queryById(goodsId);
        TbGoodsImage tbGoodsImage = tbGoodsImageDao.queryById(goodsId);
        List<TbGoodsParameter> tbGoodsParameterList = tbGoodsParameterDao.queryListById(goodsId);
        List<String> commentPreviewList = tbUserCommentDao.querySomeCommentPreview(goodsId);
        TbSeller tbSeller = tbSellerDao.queryById(tbGoods.getStoreId());
        UserGoodsDetailsDto userGoodsDetailsDto = GoodsConverter.INSTANCE.convertTbGoodsToGoodsDetailsDto(tbGoods);
        HashMap<String, String> goodsParameterMap = new HashMap<>();
        for (TbGoodsParameter x : tbGoodsParameterList) {
            goodsParameterMap.put(x.getParameterKey(), x.getParameterValue());
        }
        userGoodsDetailsDto.setStoreName(tbSeller.getStoreName());
        userGoodsDetailsDto.setGoodsVideo(tbGoodsImage.getGoodsVideo());
        userGoodsDetailsDto.setMainImages(getAllMainImages(tbGoodsImage));
        userGoodsDetailsDto.setDetailImages(getAllInfoImages(tbGoodsImage));
        userGoodsDetailsDto.setParameters(goodsParameterMap);
        userGoodsDetailsDto.setCommentPreview(commentPreviewList);
        System.out.println(userGoodsDetailsDto.toString());
        return userGoodsDetailsDto;
    }

    @Override
    public GoodsTypeDto showGoodsAllType(Integer goodsId) {
        GoodsTypeDto goodsTypeDto = new GoodsTypeDto();
        List<TbGoodsAttribute> tbGoodsAttributeList = tbGoodsAttributeDao.queryGoodsAttributeByGoodsId(goodsId);
        for (TbGoodsAttribute x: tbGoodsAttributeList) {
            List<TbGoodsType> tbGoodsTypeList = tbGoodsTypeDao.queryByAttributeId(x.getAttributeId());
            int index = x.getAttributeLevel();
            switch (index) {
                case 1: {
                    goodsTypeDto.setType1(tbGoodsTypeList);
                    goodsTypeDto.setAttribute1(x.getAttributeName());
                    break;
                }
                case 2: {
                    goodsTypeDto.setType2(tbGoodsTypeList);
                    goodsTypeDto.setAttribute2(x.getAttributeName());
                    break;
                }
                case 3: {
                    goodsTypeDto.setType3(tbGoodsTypeList);
                    goodsTypeDto.setAttribute3(x.getAttributeName());
                    break;
                }
            }
        }
        return goodsTypeDto;
    }

    @Override
    public TbGoodsInventoryPrice queryGoodsInventoryByTypeIds(UserCartAddDto userCartAddDto) {
        return tbGoodsInventoryPriceDao.queryTbGoodsInventoryPriceByTypesByTypeIds(userCartAddDto);
    }

    @Override
    public int addClickCount(TbBrowsingHistory tbBrowsingHistory) {
        // TODO 使用redis实现
        redisUtil.increment(GOODS_HISTORY_KEY + tbBrowsingHistory.getGoodsId().toString());
        Random random = new Random();
        int randomNumber = random.nextInt(10); // 生成一个0（包括）到10（不包括）之间的随机数
        if (randomNumber > 7) {
            tbGoodsDao.addClickCount(tbBrowsingHistory.getGoodsId(), (Integer)redisUtil.get(GOODS_HISTORY_KEY + tbBrowsingHistory.getGoodsId().toString()));
        }
        // tbGoodsDao.addClickCount(tbBrowsingHistory.getGoodsId());
        int i = tbBrowsingHistoryDao.updateUserBrowsing(tbBrowsingHistory);
        if (i == 0) {
            tbBrowsingHistoryDao.insert(tbBrowsingHistory);
        }
        return 1;
    }

    @Override
    public List<String> searchInBox(String searchContent) {
        List<TbGoods> tbGoodsList = tbGoodsDao.queryByGoodsInfo(searchContent);
        List<String> goodsInfoString = new ArrayList<>();
        for (TbGoods x: tbGoodsList) {
            goodsInfoString.add(x.getBrand());
        }
        return goodsInfoString;
    }

    @Override
    public TbGoodsInventoryPrice queryGoodsInventoryByCombinationId(UserCartAddDto userCartAddDto) {
        return tbGoodsInventoryPriceDao.queryGoodsInventoryByCombinationId(userCartAddDto);
    }

    public List<String> getAllMainImages(TbGoodsImage tbGoodsImage) {
        List<String> mainImages = new ArrayList<>();
        mainImages.add(tbGoodsImage.getGoodsMainImage());
        mainImages.add(tbGoodsImage.getGoodsMainImage2());
        mainImages.add(tbGoodsImage.getGoodsMainImage3());
        mainImages.add(tbGoodsImage.getGoodsMainImage4());
        mainImages.add(tbGoodsImage.getGoodsMainImage5());
        return mainImages;
    }

    public List<String> getAllInfoImages(TbGoodsImage tbGoodsImage) {
        List<String> infoImages = new ArrayList<>();
        infoImages.add(tbGoodsImage.getGoodsInfoImage1());
        infoImages.add(tbGoodsImage.getGoodsInfoImage2());
        infoImages.add(tbGoodsImage.getGoodsInfoImage3());
        infoImages.add(tbGoodsImage.getGoodsInfoImage4());
        infoImages.add(tbGoodsImage.getGoodsInfoImage5());
        infoImages.add(tbGoodsImage.getGoodsInfoImage6());
        infoImages.add(tbGoodsImage.getGoodsInfoImage7());
        infoImages.add(tbGoodsImage.getGoodsInfoImage8());
        infoImages.add(tbGoodsImage.getGoodsInfoImage9());
        infoImages.add(tbGoodsImage.getGoodsInfoImage10());
        return infoImages;
    }
}
